#version 400 core

layout(quads, equal_spacing, ccw) in;

uniform mat4 MV;
uniform mat4 P;

#define M_PI 3.14159265358979323846

//----------------------------------------------------------------------------

float Hanning(vec2 p)
{
    p -= 0.5; // map unit square to [-.5, .5]

    float r = length(p);
    r = cos(M_PI * r / 2.0);
    r *= r;

    return r;
}

//----------------------------------------------------------------------------

void main()
{
    float u = gl_TessCoord.x;
	float um = 1 - u;
	float v = gl_TessCoord.y;
	float vm = 1 - v;

    vec4 pos = um * vm * gl_in[0].gl_Position 
	             + u * vm * gl_in[1].gl_Position 
				 + u * v * gl_in[2].gl_Position
				 + um * v * gl_in[3].gl_Position;

    pos.z = Hanning(gl_TessCoord.xy);
    
    gl_Position = P * MV * pos;
}